let util = {
    /**
     * 日期格式化
     * @param {日期对象} arg1 
     * @param {格式化} arg2 
     */
    dateFormat(arg1, arg2='yyyy-MM-dd HH:mm:ss'){
        if(!arg1){
            arg1 = new Date().getTime()
        }
        let argdate;
        // console.log('日期类型 ->',Object.prototype.toString.call(arg1))
        if(typeof arg1 === 'number'){
            argdate = new Date(arg1.toString().length === 10 ? arg1*1000 : arg1);
        } else if(typeof arg1 === 'string') {
            if(arg1.indexOf('-') >- 1 || arg1.indexOf('/') > -1){
                argdate = new Date(arg1.replace(/-/g,"/"));
            } else {
                argdate = new Date(arg1.length === 10 ? parseInt(arg1)*1000 : parseInt(arg1));
            }
        } else if(arg1 instanceof Date) {
            argdate = new Date(arg1)
        } else {
            return null;
        }
        let year = argdate.getFullYear();
        let month = (argdate.getMonth() + 1)>9?(argdate.getMonth() + 1):'0'+(argdate.getMonth() + 1);
        let date = argdate.getDate()>9?argdate.getDate():'0'+argdate.getDate();
        let hours = argdate.getHours()>9?argdate.getHours():'0'+argdate.getHours();
        let minutes = argdate.getMinutes()>9?argdate.getMinutes():'0'+argdate.getMinutes();
        let seconds = argdate.getSeconds()>9?argdate.getSeconds():'0'+argdate.getSeconds();
        return arg2.replace('yyyy',year)
                .replace('MM',month)
                .replace('dd',date)
                .replace('HH', hours)
                .replace('mm',minutes)
                .replace('ss',seconds)
    },

    /**
     * 金额格式化
     * @param {金额} num 
     */
    cashFormat(num){
        if(num){
            const reg = /^[+-]?\d+(\.\d+)?$/;
            if(reg.test(num)) {
                num = parseFloat(num).toFixed(2);
                let cash_arr = num.toString().split('.');
                let cash1 = parseFloat(cash_arr[0]);
                return cash1.toLocaleString()+'.'+cash_arr[1]
            } else {
                return '非法数字'
            }
        } else {
            return '0.00'
        }
    },

    /**
     * 空间大小格式化
     * @param {空间大小} size 
     */
    sizeFormat(size=0){
        if(size > 0 && size <= 1024) {
            return '1KB'
        } else if (size > 1024 && size < 1048576) {
            //四舍五入
            return Math.round(size/1024) + 'KB'
        } else if (size >= 1048576 && size < 1073741824) {
            //四舍五入
            return Math.round(size/1048576) + 'MB'
        } else if (size >= 1073741824) {
            //四舍五入
            let num = new Number(size/1073741824)
            return num.toFixed(2) + 'GB'
        } else {
            return '0KB'
        }
    },

    /**
     * 渲染背景噪点
     * @param {节点元素} el 
     * @param {宽} width 
     * @param {高} height 
     * @param {透明度} opacity 
     */
    noiseBackground(el, width, height, opacity){
        var canvas = document.createElement("canvas");
        var context = canvas.getContext("2d");
        canvas.width = width;
        canvas.height = height;
        for( var i = 0; i < width; i++ ){
            for( var j = 0; j < height; j++ ){
                var val = Math.floor(Math.random() * 255);
                context.fillStyle = "rgba(" + val + "," + val + "," + val + "," + opacity + ")";
                context.fillRect(i, j, 1, 1);
            }
        }
        el.style.background = "url(" + canvas.toDataURL("image/png") + ")";
    },

    /**
     * 路由鉴权
     * @param {功能编码} name 
     * @param {权限组} powers 
     */
    checkRouter(name,powers){
        if(powers && powers.length > 0) {
            return powers.indexOf(name) > -1
        }
        return false
    },

    setCache(key,value){
        sessionStorage.setItem(key,value)
    },

    getCache(key){
        return sessionStorage.getItem(key)/*  != null ? decodeURI(sessionStorage.getItem(key)) : null */
    },

    phoneHide(phone) {
        let reg = /^(1[3-9][0-9])\d{4}(\d{4}$)/;
        phone = phone.replace(reg, '$1****$2');
        return phone;
    }
};
util.title = function (title) {
    title = title ? title /* + ' - ' + sessionStorage.getItem('platform')  */: 'View UI project';
    window.document.title = title;
};

export default util;
